package com.lagou.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class RabbitmqConfig {

    @Bean("dlx_queue")
    public Queue dlxQueue() {
        return QueueBuilder.durable("dlx_queue").build();
    }

    @Bean("dlx_exchange")
    public Exchange dlxExchange() {
        return ExchangeBuilder.directExchange("dlx_exchange").durable(true).build();
    }

    @Bean
    public Binding dlxBinding(@Qualifier("dlx_queue") Queue queue, @Qualifier("dlx_exchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx").noargs();
    }


    /**
     * 正常下单消费队列
     */
    @Bean("order_queue")
    public Queue orderQueue() {
        HashMap<String, Object> argumentMap = new HashMap<>(2);
        //设置消息20s过期
        argumentMap.put("x-message-ttl", 20 * 1000);
        argumentMap.put("x-dead-letter-exchange", "dlx_exchange");
        argumentMap.put("x-dead-letter-routing-key", "dlx");
        return QueueBuilder.durable("order_queue").withArguments(argumentMap).build();
    }


    /**
     * 正常下单消费交换机
     */
    @Bean("order_exchange")
    public Exchange orderExchange() {

        return ExchangeBuilder.directExchange("order_exchange").durable(true).build();
    }


    @Bean
    public Binding orderBinding(@Qualifier("order_queue") Queue queue,
                                @Qualifier("order_exchange") Exchange exchange) {

        return BindingBuilder.bind(queue).to(exchange).with("order.biz").noargs();

    }

}
